<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
    exit('Access Denied');
}

require_once('indexApp.php');

/** 用户资料管理
 * @author ZhangXiLian 2014-09-12 */
class DealController extends IndexAppController {
	
	private $DealLogic;
	
	public function __construct() {
        parent::__construct();        
        $this->DealLogic = new DealLogic();


	}
 	/** 投资记录 */
 	public function actionDefault(){

        T::$tpl->set("userItem", $this->user);

        $size = 10;
        $page = intval($_REQUEST["p"]) ? $_REQUEST["p"] : 1;
        $starttime = $_REQUEST["starttime"];
        $endtime = $_REQUEST["endtime"];
        $repaytime = intval($_REQUEST['repaytime']);

        $cond = "USER_ID=" . $this->user['ID'];
        if($starttime) $cond .= " and unix_timestamp(CREATE_TIME) > unix_timestamp('".$starttime." 00:00:00')";
        if($endtime) $cond .= " and unix_timestamp(CREATE_TIME) < unix_timestamp('".$endtime." 23:59:59')";

        switch ($repaytime) {
            case 0:break;
            case 1:$cond .= " and REPAY_TIME between 3 and 6 ";break;
            case 2:$cond .= " and REPAY_TIME between 9 and 15 ";break;
            case 3:$cond .= " and REPAY_TIME between 28 and 24 ";break;
            case 4:$cond .= " and REPAY_TIME >= 24";break;
        }

        //}
        $res = array();
        try
        {
            $res = $this->DealLogic->getDealLoadDealViewList("*, concat('a', USER_ID) as AUSER_ID, concat('a', DEAL_ID) as ADEAL_ID", $cond, "order by id desc", $page, $size);

            // 获取投资者/投资项目信息
            if(!empty($res["data"])){
                $user_arr = array();
                $products_arr = array();
                foreach($res["data"] as $k => $v){
                    $user_arr[] = $v["USER_ID"];
                    $products_arr[] = $v["DEAL_ID"];
                }
                if(!empty($user_arr)){
                    $user_str = implode(",", $user_arr);
                    $user_t = $this->Logic("UserLogic")->getUserInfoList("USERNAME, REALNAME, USERID", "USERID in (" . $user_str . ")");
                    $user_arr = array();
                    foreach($user_t["data"] as $v){
                        $user_arr["A" . $v["USERID"]] = $v;
                    }
                    T::$tpl->set("user_arr", $user_arr);
                }
                if(!empty($products_arr)){
                    $products_str = implode(",", $products_arr);
                    $products_t = $this->DealLogic->getDealList("*", "ID in (" . $products_str . ")");
                    
                    // 获取收益方式
                    $type_loan = self::$conf->confGroup("type_loan");
                   
                    foreach($products_t["data"] as $v){
                        $v["TYPE_LOAN"] = $type_loan[$v["TYPE_LOAN"]];
                        $products_arr["A" . $v["ID"]] = $v;
                        $products_arr["A" . $v["ID"]]['stime'] = strtotime($v['BAD_TIME']) - time();
                        $products_arr["A" . $v["ID"]]['loading'] = round(($v['PRICE_LOAD'] / $v['PRICE_ALLOW'])*100);
                    }
                    T::$tpl->set("products_arr", $products_arr);
                }
            }

            $par = array();
            $_REQUEST["c"] = $_REQUEST["ctl"];
            $res['page'] = page :: listPage($par, $res["count"], $page, $size);

        } catch (Exception $e)
        {
            echo $e->getMessage();
        }

        T::$tpl->set("res", $res);

        $deal_stat = self::$conf->confGroup("deal_stat");
        T::$tpl->set("deal_stat", $deal_stat);
        
    }
    /** 借款信息 */
    public function actionborrow(){

        T::$tpl->set("userItem", $this->user);

        $page = intval($_REQUEST["p"]) ? $_REQUEST["p"] : 1;
        $verify = intval($_REQUEST["verify"]);
        $size = 10;
        $cond = 'USER_ID = '.$this->user['ID'];
        switch ($verify) {
            case 1:$cond .= " and DEAL_STAT IN(1,7) ";break;
            case 2:$cond .= " and DEAL_STAT IN(2,3,4,5,6)";break;
            case 3:$cond .= " and DEAL_STAT IN(8)";break;
        }

        $res = array();
        try
        {
            $res = $this->DealLogic->getDealList("*, concat('a', USER_ID) as AUSER_ID", $cond, "order by id desc", $page, $size);
            
            // 获取借款用户等级
            if(!empty($res["data"])){
                $user_arr = array();
                foreach($res["data"] as $k => $v){
                    $user_arr[] = $v["USER_ID"];
                    $res['data'][$k]['stime'] = strtotime($v['BAD_TIME']) - time();      //倒计时
                    $res['data'][$k]['loading'] = round(($v['PRICE_LOAD'] / $v['PRICE_ALLOW'])*100);  //进度
                }
                if(!empty($user_arr)){
                    $user_str = implode(",", $user_arr);
                    $user_t = $this->Logic("UserLogic")->getUserInfoList("USERTYPE_ID, REALNAME, USERID", "USERID in (" . $user_str . ")");
                    $user_arr = array();
                    foreach($user_t["data"] as $v){
                        $v["USERTYPE_ID"] = $userType_arr["a" . $v["USERTYPE_ID"]];
                        $user_arr["A" . $v["USERID"]] = $v;
                    }
                    T::$tpl->set("user_arr", $user_arr);
                }
            }
            
            $par = array();
            $_REQUEST["c"] = $_REQUEST["ctl"];
            $res['page'] = page :: listPage($par, $res["count"], $page, $size);
        } catch (Exception $e)
        {
            echo $e->getMessage();
        }
        T::$tpl->set("res", $res);
        
        // 获取收益方式
        $type_loan = self::$conf->confGroup("type_loan");
        T::$tpl->set("type_loan", $type_loan);
        $deal_stat = self::$conf->confGroup("deal_stat");
        T::$tpl->set("deal_stat", $deal_stat);
        
    }
    /** 还款记录 */
    public function actionrepay(){

        T::$tpl->set("userItem", $this->user);

        $page = intval($_REQUEST["p"]) ? $_REQUEST["p"] : 1;
        $size = 10;
        $cond = "1=1 ";
        //$pid = intval($_REQUEST["pid"]) ? $_REQUEST["pid"] : 0;
        //if(isset($pid) && !empty($pid)){
            $cond .= " and USER_ID=" . $this->user['ID'];
        //}
        $res = array();
        try
        {
            $res = $this->DealLogic->getDealLoadDealViewList("*, concat('a', USER_ID) as AUSER_ID, concat('a', DEAL_ID) as ADEAL_ID", $cond, "order by id desc", $page, $size);

            // 获取投资者/投资项目信息
            if(!empty($res["data"])){
                $user_arr = array();
                $products_arr = array();
                foreach($res["data"] as $k => $v){
                    $products_arr[] = $v["DEAL_ID"];
                }
                if(!empty($products_arr)){
                    $products_str = implode(",", $products_arr);
                    $products_t = $this->DealLogic->getDealList("*", "ID in (" . $products_str . ")");
                    
                    // 获取收益方式
                    $type_loan = self::$conf->confGroup("type_loan");
                   
                    foreach($products_t["data"] as $v){
                        $v["TYPE_LOAN"] = $type_loan[$v["TYPE_LOAN"]];
                        $products_arr["A" . $v["ID"]] = $v;
                    }
                    T::$tpl->set("products_arr", $products_arr);
                }
            }

            $par = array();
            $_REQUEST["c"] = $_REQUEST["ctl"];
           // $res['page'] = page :: listPage($par, $res["count"], $page, $size);
        } catch (Exception $e)
        {
            echo $e->getMessage();
        }
        print_r($res);
        T::$tpl->set("res", $res);
        
    }

    /** 同意批准金额 */
    public function actionAgree(){
        T::$tplFile='public/empty';
        T::$tpl->set("userItem", $this->user);

        $dealid = intval($_REQUEST["id"]);
        if($dealid){
            $products = $this->DealLogic->getRecordsItem("*", "ID=".$dealid);
            
        }else{
            exit('0');
        }

        
            
        
    }
}//End CLass
?>
